package com.itheima.service.mongo.impl;

import cn.hutool.core.util.ObjectUtil;
import com.itheima.domain.mongo.RecommendUser;
import com.itheima.domain.mongo.Visitor;
import com.itheima.service.mongo.RecommendUserService;
import com.itheima.service.mongo.VisitorService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * @Author 请不要叫我高司令
 * @Date 2022/3/20 23:07
 * @Version 1.0
 */

@DubboService
public class VisitorServiceImpl implements VisitorService {


    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private RecommendUserService recommendUserService;


    //历史访客
    @Override
    public List<Visitor> findVisitors(Long userId, Long lastAccessTime) {

        //根据时间排序
        Query query = new Query(
                Criteria.where("userId").is(userId)
                        .and("date").gt(lastAccessTime)
        ).with(Sort.by(Sort.Order.desc("date")))
                .skip(0).limit(5);

        return mongoTemplate.find(query, Visitor.class);


    }

    //保存用户访问记录
    @Override
    public void saveVisitors(Long visitorUserId, Long userId, Integer commentType) {

        //排除自己访问自己
        if (visitorUserId == userId) {
            return;
        }

        Visitor visitor = new Visitor();
        //查询匹配值
        RecommendUser personalInfo = recommendUserService.findPersonalInfo(userId, visitorUserId);
        if (ObjectUtil.isNotEmpty(personalInfo)) {
            visitor.setScore(personalInfo.getScore());
        } else {
            visitor.setScore(100D);
        }

        //封装对象
        switch (commentType) {
            case 0:{//查看动态
                visitor.setDate(System.currentTimeMillis());
                visitor.setUserId(userId);
                visitor.setVisitorUserId(visitorUserId);
                visitor.setFrom("查看动态");
                mongoTemplate.save(visitor);
                break;
            }
            case 1:{//对动态点赞
                visitor.setDate(System.currentTimeMillis());
                visitor.setUserId(userId);
                visitor.setVisitorUserId(visitorUserId);
                visitor.setFrom("动态点赞");
                mongoTemplate.save(visitor);
                break;
            }
            case 2:{//对动态评论
                visitor.setDate(System.currentTimeMillis());
                visitor.setUserId(userId);
                visitor.setVisitorUserId(visitorUserId);
                visitor.setFrom("动态评论");
                mongoTemplate.save(visitor);
                break;
            }
            case 3:{//对动态喜欢
                visitor.setDate(System.currentTimeMillis());
                visitor.setUserId(userId);
                visitor.setVisitorUserId(visitorUserId);
                visitor.setFrom("动态喜欢");
                mongoTemplate.save(visitor);
                break;
            }
            case 4:{//对视频点赞

            }
            case 5:{//对视频评论

            }
        }
    }
}
















